package edu.gatech.cc.CFAWaffleFriesWheresMyStuff.gui;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.R;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore.UserDoesNotExistException;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.DatastoreFactory;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Item;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.ItemManager;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.User;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.UserManager;

public class FindMatchesActivity extends ListActivity {
	
	private User _user;
	private ItemManager _itemManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_matches);
		Bundle extras = getIntent().getExtras();
		
		Datastore ds = DatastoreFactory.getDatastore(getApplicationContext());
		
		try {
			_user = (new UserManager(ds)).getUser(extras.getString("user", ""));
		} catch (UserDoesNotExistException e) {
			e.printStackTrace();
		}
		
		_itemManager = new ItemManager(ds);
		
		PickItem();
		
		((ImageButton)findViewById(R.id.find_matches_search_button)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PickItem();
			}
		});
		
	}
	
	protected void PickItem() {
		final List<Item> items = _itemManager.getUserItems(_user.getUsername());
		if(items.size() == 0)
		{
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle("Woah there!");
			builder.setMessage("You must add items before you can find any matches!");
			builder.setCancelable(false);
			builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					FindMatchesActivity.this.finish();
				}
			});
			builder.create().show();
		} else {
			String[] itemNames = new String[items.size()];
			for(int i = 0; i < items.size(); i++)
				itemNames[i] = items.get(i).getName();
			
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle("Pick an Item to Match");
			builder.setItems(itemNames, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Item itemToMatch = items.get(which);
					List<Item> matches = _itemManager.getMatchesToItem(itemToMatch);
					setListAdapter(new ItemMatchesAdapter(FindMatchesActivity.this, R.layout.item_list_row, matches));
				}
			});
			builder.create().show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.find_matches, menu);
		return true;
	}

	private class ItemMatchesAdapter extends ArrayAdapter<Item> {
		private List<Item> items;
		
		public ItemMatchesAdapter(Context context, int textViewResourceId, List<Item> items)
		{
			super(context, textViewResourceId, items);
			this.items = items;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.item_list_row, null);
            }
            Item item = items.get(position);
            if (item != null) {
                    TextView tt = (TextView) v.findViewById(R.id.itemlist_row_name);
                    TextView bt = (TextView) v.findViewById(R.id.itemlist_row_summary);
                    
                    if (tt != null) {
                          tt.setText(item.getName());
                    }
                    if(bt != null){
                          bt.setText(item.getDescription());
                    }
            }
            return v;
		}
	}
}
